set linesize 140
set rmsg on
set scheme s1mono
/***************************************************************************
	Project		:	Army Service in the All-Volunteer Era	
	Author(s)	:	Kyle Greenberg	(kyle.greenberg@westpoint.edu)
					Matthew Gudgeon (matthew.gudgeon@westpoint.edu)
					Adam Isen 		(Adam.Isen@treasury.gov)
					Corbin Miller 	(Corbin.Miller@treasury.gov)
					Rich Patterson 	(rich_patterson@byu.edu)
	File Name	:	iv.do
	Description	:	This file runs the main 2SLS analysis for the paper
****************************************************************************/

*---- if not running master, set up file structure ----*
*change directory to where programs and subfolders are stored
*cd ""

if "${raw}"==""		global raw		"raw/"
if "${data}"==""	global data		"data/"
if "${output}"==""	global output	"output/"

cap mkdir	"${output}"
cap mkdir	"${output}iv/"

*---- select subprograms ----*
local iv_regs		1
local iv_tabs		1
local iv_figs		1

local outcomelist wages self rawwage anywages anyf1098T anyf1098 married military anyf1040 public zip_income mortality cf_wages cf_anywages cf_married cf_anyf1098 cf_90_92 cf_93_95 cf_96_98 cf_99_01 cf_02_04 cf_05_08
local stratalist all black white

*make macros for labels
local anywages_l	"Employed"
local cf_anywages_l	"Employed"
local wages_l		"Earnings (Dollars)"
local cf_wages_l	"Earnings (Dollars)"
local anyf1098T_l 	"Receives Form 1098-T (College)"
local anyf1098_l	"Receives Form 1098 (Homeownership)"
local cf_anyf1098_l	"Receives Form 1098 (Homeownership)"
local married_l		"Married on Form 1040"
local cf_married_l	"Married on Form 1040"
local self_l		"Self-Employment Earnings (Dollars)"
local rawwage_l		"Earnings (Dollars)"
local military_l	"Receives Military W-2"
local anyf1040_l	"Files Form 1040"
local public_l		"In Public Sector"
local zip_income_l	"Average Zip Code Income"
local mortality		"Mortality"

*make macros for display format
foreach x in wages cf_wages self rawwage zip_income{
	local `x'_f %9.0fc
}
foreach x in anywages cf_anywages anyf1098T anyf1098 cf_anyf1098 married cf_married military anyf1040 public mortality {
	local `x'_f %5.3f
}

*-------------------------------*
*----	2SLS regressions	----*
*-------------------------------*
if `iv_regs'==1{
	foreach strata of local stratalist{
		foreach outcome of local outcomelist {
		
			*---- initialize output file ----*
			clear
			set obs 21
			gen y = _n - 2
			foreach cut in 31 50{
				foreach x in b se N m{
					gen `x'_`strata'_`outcome'_`cut'_iv = .
				}
			}
			save "${output}iv/iv_results_`strata'_`outcome'", replace
			
			*---- loop over years relative to first AFQT ----*
			forval y=-1/19 {
				local yt = `y'
				if `y' == -1 local yt m1
				
				if "`outcome'"=="mortality" & `y'<1 di "skipping `outcome' for t=`y'"
				else{

					*---- regressions ----*
					use `outcome'_`yt' firstafqt firstafqt_fy firstyear inst31 k31* instk31* inst50 k50* instk50* access quarterFE `stratalist' using "${data}army-treasury-analysis" if `strata'==1, clear
					qui sum `outcome'_`yt'
					if r(N)==0 di "*---- skipping `outcome'_`yt' because it doesn't have observations ----*"
					else{
						*1st cutoff (AFQT=31)
						ivregress 2sls `outcome'_`yt' k31 instk31 k31_2 instk31_2 (access=inst31) ib175.quarter if inrange(firstafqt,12,49), robust
						local b_31 = _b[access]
						local se_31 = _se[access]
						local N_31 = e(N)
						sum `outcome'_`yt' if e(sample)
						local m_31 = r(mean)
							
						*2nd cutoff (AFQT=50)
						ivregress 2sls `outcome'_`yt' k50 instk50 k50_2 instk50_2 (access=inst50) ib175.quarter if inrange(firstafqt,31,68), robust
						local b_50 = _b[access]
						local se_50 = _se[access]
						local N_50 = e(N)
						sum `outcome'_`yt' if e(sample)		
						local m_50 = r(mean)
						
						*---- write estimates to dataset ----*
						use "${output}iv/iv_results_`strata'_`outcome'", clear
						foreach cut in 31 50{
							foreach x in b se N m{
								replace `x'_`strata'_`outcome'_`cut'_iv = ``x'_`cut'' if y == `y'
							}
						}
						save "${output}iv/iv_results_`strata'_`outcome'", replace
					}
				}
			}
		}
	}
}	// end if
else di "*---- skipping 2SLS regressions----*"

*---------------------------*
*---- ouput 2SLS tables ----*
*---------------------------*
if `iv_tabs'==1{
	
	*---- 2SLS RD Estimates For Main Outcomes ----*
	use "${output}iv/iv_results_all_wages", clear
	merge 1:1 y using "${output}iv/iv_results_all_anywages", nogen
	merge 1:1 y using "${output}iv/iv_results_all_anyf1098T", nogen
	
	*set significance stars
	forval y=-1/19 {
		local n = `y'+2
		foreach outcome in wages anywages anyf1098T {
			foreach cut in 31 50 {
				if abs(b_all_`outcome'_31_iv[`n']/se_all_`outcome'_31_iv[`n']) < 1.645	local star_`outcome'_`cut' = "" 
				if abs(b_all_`outcome'_31_iv[`n']/se_all_`outcome'_31_iv[`n']) >= 1.645	local star_`outcome'_`cut' = "*" 
				if abs(b_all_`outcome'_31_iv[`n']/se_all_`outcome'_31_iv[`n']) >= 1.96 	local star_`outcome'_`cut' = "**" 
				if abs(b_all_`outcome'_31_iv[`n']/se_all_`outcome'_31_iv[`n']) >= 2.576 local star_`outcome'_`cut' = "***" 
			}
		}
	}

	*write to table
	file open iv using "${output}iv/iv_results_all.tex", write replace
	file write iv "\begin{tabular}{l*{8}c}" _n "\hline \hline" _n
	file write iv "Years Since App & Earnings & Earnings & Emp. & Emp. & 1098-T & 1098-T  & Any Dis. & Any Dis. \\" _n
	file write iv "AFQT Cutoff: & 31 & 50 & 31 & 50 & 31 & 50 & 31 & 50 \\" _n
	file write iv "& (1) & (2) & (3)  & (4) & (5) & (6) & (7) & (8) \\ \cline{2-9}" _n "\\" _n
	forval y=-1/19 {
		local n = `y'+2

		*beta
		file write iv "`y'" 
		foreach outcome in wages anywages anyf1098T {
			if "`outcome'"=="wages" local fmt %6.0fc
			else local fmt %5.3f
			file write iv "&" `fmt' (b_all_`outcome'_31_iv[`n']) "`star_`outcome'_31'"
			file write iv "&" `fmt' (b_all_`outcome'_50_iv[`n']) "`star_`outcome'_50'"
		}
		file write iv "\\" _n
		
		*se
		foreach cut in 31 50 {
			local temp = floor(se_all_wages_`cut'_iv[`n'])
			local length = length(trim("`temp'"))
			if inrange(`length',1,3) local `cut'_f "%`length'.0fc"
			local ++length
			if inrange(`length',4,6) local `cut'_f "%`length'.0fc"
			local ++length
			if inrange(`length',7,9) local `cut'_f "%`length'.0fc"
		}
		file write iv "" 
		foreach outcome in wages anywages anyf1098T {
			if "`outcome'"=="wages" local fmt `31_f'
			else local fmt %5.3f
			file write iv "&" "(" `fmt' (se_all_`outcome'_31_iv[`n']) ")" 
			if "`outcome'"=="wages" local fmt `50_f'
			else local fmt %5.3f
			file write iv "&" "(" `fmt' (se_all_`outcome'_50_iv[`n']) ")" 
		}
		file write iv "\\" _n

		*N
		file write iv "N"
		foreach outcome in wages anywages anyf1098T {
			foreach cut in 31 50 {
				local temp = floor(N_all_`outcome'_`cut'_iv[`n'])
				local length = length(trim("`temp'"))
				if inrange(`length',1,3) local `outcome'_`cut'_f "%`length'.0fc"
				local ++length
				if inrange(`length',4,6) local `outcome'_`cut'_f "%`length'.0fc"
				local ++length
				if inrange(`length',7,9) local `outcome'_`cut'_f "%`length'.0fc"
				file write iv "&" "[" ``outcome'_`cut'_f' (N_all_`outcome'_`cut'_iv[`n']) "]"
			}
		}
		file write iv "\\" _n
	}
	file write iv "\hline \hline" _n "\end{tabular}" _n
	file close iv

	*---- mortality table - overall ----*
	use "${output}iv/iv_results_all_mortality", clear
	
	file open iv using "${output}iv/iv_mortality_all.tex", write replace
	file write iv "\begin{tabular}{l*{6}c}" _n "\hline\hline" _n
	file write iv "& Died w/in & Died w/in & Died w/in & Died w/in & Died w/in & Died w/in \\" _n
	file write iv "& 1 Year & 3 Years  & 5 Years & 10 Years & 15 Years & 19 years  \\" _n
	file write iv "& (1) & (2) & (3) & (4) & (5) & (6) \\ \cline{2-7}" _n
	file write iv "\\" _n "& \multicolumn{6}{c}{\underline{Panel (a): 31 AFQT Cutoff}} \\ \\" _n
	file write iv "Enlist " "&" %5.3f (b_all_mortality_31_iv[3]) "&" %5.3f (b_all_mortality_31_iv[5]) "&" %5.3f (b_all_mortality_31_iv[7]) "&" %5.3f (b_all_mortality_31_iv[12]) "&" %5.3f (b_all_mortality_31_iv[17]) "&" %5.3f (b_all_mortality_31_iv[21]) "\\" _n
	file write iv " " "&(" %5.3f (se_all_mortality_31_iv[3]) ")&(" %5.3f (se_all_mortality_31_iv[5]) ")&(" %5.3f (se_all_mortality_31_iv[7]) ")&(" %5.3f (se_all_mortality_31_iv[12]) ")&(" %5.3f (se_all_mortality_31_iv[17]) ")&(" %5.3f (se_all_mortality_31_iv[21]) ") \\" _n
	file write iv "Observations " "&" %9.0fc (N_all_mortality_31_iv[3]) "&" %9.0fc (N_all_mortality_31_iv[5]) "&" %9.0fc (N_all_mortality_31_iv[7]) "&" %9.0fc (N_all_mortality_31_iv[12]) "&" %9.0fc (N_all_mortality_31_iv[17]) "&" %9.0fc (N_all_mortality_31_iv[21]) "\\" _n
	file write iv "Dep. Var. Mean" "&" %5.3f (m_all_mortality_31_iv[3]) %5.3f (m_all_mortality_31_iv[5]) %5.3f (m_all_mortality_31_iv[7]) %5.3f (m_all_mortality_31_iv[12]) %5.3f (m_all_mortality_31_iv[17]) %5.3f (m_all_mortality_31_iv[21]) "\\" _n
	file write iv "\\" _n "& \multicolumn{6}{c}{\underline{Panel (b): 50 AFQT Cutoff}} \\ \\" _n
	file write iv "Enlist " "&" %5.3f (b_all_mortality_50_iv[3]) "&" %5.3f (b_all_mortality_50_iv[5]) "&" %5.3f (b_all_mortality_50_iv[7]) "&" %5.3f (b_all_mortality_50_iv[12]) "&" %5.3f (b_all_mortality_50_iv[17]) "&" %5.3f (b_all_mortality_50_iv[21]) "\\" _n
	file write iv " " "&(" %5.3f (se_all_mortality_50_iv[3]) ")&(" %5.3f (se_all_mortality_50_iv[5]) ")&(" %5.3f (se_all_mortality_50_iv[7]) ")&(" %5.3f (se_all_mortality_50_iv[12]) ")&(" %5.3f (se_all_mortality_50_iv[17]) ")&(" %5.3f (se_all_mortality_50_iv[21]) ") \\" _n
	file write iv "Observations " "&" %9.0fc (N_all_mortality_50_iv[3]) "&" %9.0fc (N_all_mortality_50_iv[5]) "&" %9.0fc (N_all_mortality_50_iv[7]) "&" %9.0fc (N_all_mortality_50_iv[12]) "&" %9.0fc (N_all_mortality_50_iv[17]) "&" %9.0fc (N_all_mortality_50_iv[21]) "\\" _n
	file write iv "Dep. Var. Mean" "&" %5.3f (m_all_mortality_50_iv[3]) %5.3f (m_all_mortality_50_iv[5]) %5.3f (m_all_mortality_50_iv[7]) %5.3f (m_all_mortality_50_iv[12]) %5.3f (m_all_mortality_50_iv[17]) %5.3f (m_all_mortality_50_iv[21]) "\\" _n
	file write iv "\\" _n "\hline \hline" _n "\end{tabular}" _n
	file close iv

	*---- mortality table - by race ----*
	use "${output}iv/iv_results_black_mortality", clear
	merge 1:1 y using "${output}iv/iv_results_white_mortality", nogen
	
	file open iv using "${output}iv/iv_mortality_race.tex", write replace
	file write iv "\begin{tabular}{l*{6}c}" _n "\hline\hline" _n
	file write iv "& Died w/in & Died w/in & Died w/in & Died w/in & Died w/in & Died w/in \\" _n
	file write iv "& 1 Year & 3 Years  & 5 Years & 10 Years & 15 Years & 19 years  \\" _n
	file write iv "& (1) & (2) & (3) & (4) & (5) & (6) \\ \cline{2-7}" _n
	file write iv "\\" _n "& \multicolumn{6}{c}{\underline{Panel (a): 31 AFQT Cutoff Black}} \\ \\" _n
	file write iv "Enlist " "&" %5.3f (b_black_mortality_31_iv[3]) "&" %5.3f (b_black_mortality_31_iv[5]) "&" %5.3f (b_black_mortality_31_iv[7]) "&" %5.3f (b_black_mortality_31_iv[12]) "&" %5.3f (b_black_mortality_31_iv[17]) "&" %5.3f (b_black_mortality_31_iv[21]) "\\" _n
	file write iv " " "&(" %5.3f (se_black_mortality_31_iv[3]) ")&(" %5.3f (se_black_mortality_31_iv[5]) ")&(" %5.3f (se_black_mortality_31_iv[7]) ")&(" %5.3f (se_black_mortality_31_iv[12]) ")&(" %5.3f (se_black_mortality_31_iv[17]) ")&(" %5.3f (se_black_mortality_31_iv[21]) ") \\" _n
	file write iv "Observations " "&" %9.0fc (N_black_mortality_31_iv[3]) "&" %9.0fc (N_black_mortality_31_iv[5]) "&" %9.0fc (N_black_mortality_31_iv[7]) "&" %9.0fc (N_black_mortality_31_iv[12]) "&" %9.0fc (N_black_mortality_31_iv[17]) "&" %9.0fc (N_black_mortality_31_iv[21]) "\\" _n
	file write iv "Dep. Var. Mean" "&" %5.3f (m_black_mortality_31_iv[3]) %5.3f (m_black_mortality_31_iv[5]) %5.3f (m_black_mortality_31_iv[7]) %5.3f (m_black_mortality_31_iv[12]) %5.3f (m_black_mortality_31_iv[17]) %5.3f (m_black_mortality_31_iv[21]) "\\" _n
	file write iv "\\" _n "& \multicolumn{6}{c}{\underline{Panel (b): 31 AFQT Cutoff White}} \\ \\" _n
	file write iv "Enlist " "&" %5.3f (b_white_mortality_31_iv[3]) "&" %5.3f (b_white_mortality_31_iv[5]) "&" %5.3f (b_white_mortality_31_iv[7]) "&" %5.3f (b_white_mortality_31_iv[12]) "&" %5.3f (b_white_mortality_31_iv[17]) "&" %5.3f (b_white_mortality_31_iv[21]) "\\" _n
	file write iv " " "&(" %5.3f (se_white_mortality_31_iv[3]) ")&(" %5.3f (se_white_mortality_31_iv[5]) ")&(" %5.3f (se_white_mortality_31_iv[7]) ")&(" %5.3f (se_white_mortality_31_iv[12]) ")&(" %5.3f (se_white_mortality_31_iv[17]) ")&(" %5.3f (se_white_mortality_31_iv[21]) ") \\" _n
	file write iv "Observations " "&" %9.0fc (N_white_mortality_31_iv[3]) "&" %9.0fc (N_white_mortality_31_iv[5]) "&" %9.0fc (N_white_mortality_31_iv[7]) "&" %9.0fc (N_white_mortality_31_iv[12]) "&" %9.0fc (N_white_mortality_31_iv[17]) "&" %9.0fc (N_white_mortality_31_iv[21]) "\\" _n
	file write iv "Dep. Var. Mean" "&" %5.3f (m_white_mortality_31_iv[3]) %5.3f (m_white_mortality_31_iv[5]) %5.3f (m_white_mortality_31_iv[7]) %5.3f (m_white_mortality_31_iv[12]) %5.3f (m_white_mortality_31_iv[17]) %5.3f (m_white_mortality_31_iv[21]) "\\" _n
	file write iv "\\" _n "& \multicolumn{6}{c}{\underline{Panel (c): 50 AFQT Cutoff Black}} \\ \\" _n
	file write iv "Enlist " "&" %5.3f (b_black_mortality_50_iv[3]) "&" %5.3f (b_black_mortality_50_iv[5]) "&" %5.3f (b_black_mortality_50_iv[7]) "&" %5.3f (b_black_mortality_50_iv[12]) "&" %5.3f (b_black_mortality_50_iv[17]) "&" %5.3f (b_black_mortality_50_iv[21]) "\\" _n
	file write iv " " "&(" %5.3f (se_black_mortality_50_iv[3]) ")&(" %5.3f (se_black_mortality_50_iv[5]) ")&(" %5.3f (se_black_mortality_50_iv[7]) ")&(" %5.3f (se_black_mortality_50_iv[12]) ")&(" %5.3f (se_black_mortality_50_iv[17]) ")&(" %5.3f (se_black_mortality_50_iv[21]) ") \\" _n
	file write iv "Observations " "&" %9.0fc (N_black_mortality_50_iv[3]) "&" %9.0fc (N_black_mortality_50_iv[5]) "&" %9.0fc (N_black_mortality_50_iv[7]) "&" %9.0fc (N_black_mortality_50_iv[12]) "&" %9.0fc (N_black_mortality_50_iv[17]) "&" %9.0fc (N_black_mortality_50_iv[21]) "\\" _n
	file write iv "Dep. Var. Mean" "&" %5.3f (m_black_mortality_50_iv[3]) %5.3f (m_black_mortality_50_iv[5]) %5.3f (m_black_mortality_50_iv[7]) %5.3f (m_black_mortality_50_iv[12]) %5.3f (m_black_mortality_50_iv[17]) %5.3f (m_black_mortality_50_iv[21]) "\\" _n
	file write iv "\\" _n "& \multicolumn{6}{c}{\underline{Panel (d): 50 AFQT Cutoff White}} \\ \\" _n
	file write iv "Enlist " "&" %5.3f (b_white_mortality_50_iv[3]) "&" %5.3f (b_white_mortality_50_iv[5]) "&" %5.3f (b_white_mortality_50_iv[7]) "&" %5.3f (b_white_mortality_50_iv[12]) "&" %5.3f (b_white_mortality_50_iv[17]) "&" %5.3f (b_white_mortality_50_iv[21]) "\\" _n
	file write iv " " "&(" %5.3f (se_white_mortality_50_iv[3]) ")&(" %5.3f (se_white_mortality_50_iv[5]) ")&(" %5.3f (se_white_mortality_50_iv[7]) ")&(" %5.3f (se_white_mortality_50_iv[12]) ")&(" %5.3f (se_white_mortality_50_iv[17]) ")&(" %5.3f (se_white_mortality_50_iv[21]) ") \\" _n
	file write iv "Observations " "&" %9.0fc (N_white_mortality_50_iv[3]) "&" %9.0fc (N_white_mortality_50_iv[5]) "&" %9.0fc (N_white_mortality_50_iv[7]) "&" %9.0fc (N_white_mortality_50_iv[12]) "&" %9.0fc (N_white_mortality_50_iv[17]) "&" %9.0fc (N_white_mortality_50_iv[21]) "\\" _n
	file write iv "Dep. Var. Mean" "&" %5.3f (m_white_mortality_50_iv[3]) %5.3f (m_white_mortality_50_iv[5]) %5.3f (m_white_mortality_50_iv[7]) %5.3f (m_white_mortality_50_iv[12]) %5.3f (m_white_mortality_50_iv[17]) %5.3f (m_white_mortality_50_iv[21]) "\\" _n
	file write iv "\\" _n "\hline \hline" _n "\end{tabular}" _n
	file close iv


}	//end if
else di "*---- skip outputting 2SLS tables ----*"

*----------------------------*
*---- ouput 2SLS figures ----*
*----------------------------*
if `iv_figs'==1{
	foreach strata of local stratalist {
		foreach outcome of local outcomelist {
			*bring in results
			use "${output}iv/iv_results_`strata'_`outcome'", clear
			keep if inrange(y,-1,19)
					
			*create plotting variables
			gen y1 = y-.1
			gen y2 = y+.2
			foreach cut in 31 50 {
				gen hi_`strata'_`outcome'_`cut'_iv = b_`strata'_`outcome'_`cut'_iv + 1.96*se_`strata'_`outcome'_`cut'_iv
				gen lo_`strata'_`outcome'_`cut'_iv = b_`strata'_`outcome'_`cut'_iv - 1.96*se_`strata'_`outcome'_`cut'_iv
			}
			
			*plot estimates
			tw 	(rcap hi_`strata'_`outcome'_31_iv lo_`strata'_`outcome'_31_iv y1, lcolor(black) lp(dash) lw(thin))	///
				(line b_`strata'_`outcome'_31_iv y1, lcolor(black) lp(dash) lw(medthick))	///
				(rcap hi_`strata'_`outcome'_50_iv lo_`strata'_`outcome'_50_iv y2, lcolor(gs7) lw(thin))	///
				(line b_`strata'_`outcome'_50_iv y2, lcolor(gs7) lw(medthick))	///
				, yline(0, lpattern(dot)) xline(0, lpattern(dot))	///
				ytitle("``outcome'_l'") ylabel(,angle(0) format(``outcome'_f'))	///
				xtitle("Years Relative to Application") xlabel(-1(2)19) xtick(-1(1)19)	///
				legend(size(small) order (2 4) label(2 "Cutoff = 31") label(4 "Cutoff = 50"))
			graph export "${output}iv/iv_`strata'_`outcome'.pdf", as(pdf) replace
		}
	}

	*---- compare black and white ----*
	foreach outcome of local outcomelist {
		use "${output}iv/iv_results_black_`outcome'", clear
		merge 1:1 y using "${output}iv/iv_results_white_`outcome'", nogen
		gen y1 = y-0.1
		gen y2 = y+0.2
		foreach cut in 31 50 {
			gen hi_black_`outcome'_`cut'_iv = b_black_`outcome'_`cut'_iv + 1.96*se_black_`outcome'_`cut'_iv
			gen lo_black_`outcome'_`cut'_iv = b_black_`outcome'_`cut'_iv - 1.96*se_black_`outcome'_`cut'_iv
			gen hi_white_`outcome'_`cut'_iv = b_white_`outcome'_`cut'_iv + 1.96*se_white_`outcome'_`cut'_iv
			gen lo_white_`outcome'_`cut'_iv = b_white_`outcome'_`cut'_iv - 1.96*se_white_`outcome'_`cut'_iv
			tw 	(rcap hi_black_`outcome'_`cut'_iv lo_black_`outcome'_`cut'_iv y1, lcolor(gs7) lp(longdash) lw(thin))	///
				(line b_black_`outcome'_`cut'_iv y1, lcolor(gs7) lp(longdash) lw(medthick))	///
				(rcap hi_white_`outcome'_`cut'_iv lo_white_`outcome'_`cut'_iv y2, lcolor(black) lp(solid) lw(thin))	///
				(line b_white_`outcome'_`cut'_iv y2, lcolor(black) lp(shortdash) lw(medthick))	///
				, yline(0, lpattern(dot)) xline(0, lpattern(dot))	///
				ytitle("``outcome'_l'") ylabel(,angle(0) format(``outcome'_f'))	///
				xtitle("Years Relative to Application") xlabel(-1(2)19) xtick(-1(1)19)	///
				legend(size(small) order ( 2 "Black (Cutoff=`cut')" 4 "White (Cutoff=`cut')"))
			graph export "${output}iv/iv_race_`outcome'_`cut'.pdf", as(pdf) replace
		}
	}

	*---- average enlisted and non-enlisted complier earnings ----*
	use "${output}iv/iv_results_white_cf_wages", clear
	merge 1:1 y using "${output}iv/iv_results_black_cf_wages", nogen
	merge 1:1 y using "${output}iv/iv_results_white_wages", nogen
	merge 1:1 y using "${output}iv/iv_results_black_wages", nogen

	keep if inrange(y,0,19)
	gen y1 = y-0.1
	gen y2 = y+0.2

	foreach cut in 31 50 {
		gen post_black_`cut' = b_black_wages_`cut'_iv + b_black_cf_wages_`cut'_iv
		gen post_white_`cut' = b_white_wages_`cut'_iv + b_white_cf_wages_`cut'_iv
		tw (connected b_white_cf_wages_`cut'_iv y2, mcolor(blue%25) msymbol(o) lcolor(blue%25) lp(longdash) lw(medthick))	///
			(connected post_white_`cut' y2, mc(blue) m(O) msty(o) lcolor(blue) lp(longdash) lw(medthick)) ///
			(connected b_black_cf_wages_`cut'_iv y1, mc(maroon%25) m(t) lcolor(maroon%25) lp(solid) lw(medthick))	///
			(connected post_black_`cut' y1, mc(maroon) m(T) lcolor(maroon) lp(solid) lw(medthick)), ///
			legend(label(1 "White (Not Enlisted)") label(2 "White (Enlisted)") label(3 "Black (Not Enlisted)") label(4 "Black (Enlisted)"))	///
			xtitle("Years Relative to Application") xtick(0(1)19) xlabel(1(2)19)	///
			ytitle("Earnings (Dollars)") ylabel(10000(10000)50000, format(%6.0fc)) ytick(10000(10000)50000) 
		graph export "${output}iv/complier_earnings_`cut'.pdf", as(pdf) replace
	}

	*---- counterfactual earnings by 3-year cohort groups ----*
	use "${output}iv/iv_results_black_cf_90_92", clear
	merge 1:1 y using "${output}iv/iv_results_black_cf_93_95", nogen
	merge 1:1 y using "${output}iv/iv_results_black_cf_96_98", nogen
	merge 1:1 y using "${output}iv/iv_results_black_cf_99_01", nogen
	merge 1:1 y using "${output}iv/iv_results_black_cf_02_04", nogen
	merge 1:1 y using "${output}iv/iv_results_black_cf_05_08", nogen
	merge 1:1 y using "${output}iv/iv_results_white_cf_90_92", nogen
	merge 1:1 y using "${output}iv/iv_results_white_cf_93_95", nogen
	merge 1:1 y using "${output}iv/iv_results_white_cf_96_98", nogen
	merge 1:1 y using "${output}iv/iv_results_white_cf_99_01", nogen
	merge 1:1 y using "${output}iv/iv_results_white_cf_02_04", nogen
	merge 1:1 y using "${output}iv/iv_results_white_cf_05_08", nogen
	
	keep if y==10
	rename (*_cf_90_92*_iv *_cf_93_95*_iv *_cf_96_98*_iv *_cf_99_01*_iv *_cf_02_04*_iv *_cf_05_08*_iv) (**90_92 **93_95 **96_98 **99_01 **02_04 **05_08)
	drop N_* m_*
	reshape long b_black_31 se_black_31 b_black_50 se_black_50 b_white_31 se_white_31 b_white_50 se_white_50, i(y) j(cohort) string

	foreach race in black white {
		foreach cut in 31 50 {
			gen hi_`race'_`cut' = b_`race'_`cut' + 1.96*se_`race'_`cut'
			gen lo_`race'_`cut' = b_`race'_`cut' - 1.96*se_`race'_`cut'
		}
	}

	gen y1=_n-.1
	gen y2=_n+.2
	
	foreach cut in 31 50 {
		tw (rcap hi_black_`cut' lo_black_`cut' y1, lpattern(longdash) lcolor(gs7))	///
			(scatter b_black_`cut' y1, mcolor(gs7))	///
			(rcap hi_white_`cut' lo_white_`cut' y2, lpattern(solid) lcolor(black))	///
			(scatter b_white_`cut' y2, mcolor(black)),	///
			ytitle("Earnings (Dollars)") ylabel(, angle(0) format(%6.0fc))	///
			xtitle("Application Cohort Group") xlabel(1 "90-92" 2 "93-95" 3 "96-98" 4 "99-01" 5 "02-04" 6 "05-08")	///
			legend( order( 1 "Black (Cutoff = 31)" 3 "White (Cutoff = 31)" ) )
		graph export "${output}iv/counterfactual_earnings_`cut'.pdf", as(pdf) replace		
	}

}	//end if
else di "*---- skip outputting 2SLS figures ----*"
